package com.xh.bussiness.resource.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.resource.bean.dto.MaterialDto;
import com.xh.bussiness.resource.bean.request.MaterialPrepareRequest;
import com.xh.bussiness.resource.constant.MaterialType;
import com.xh.bussiness.resource.model.MaterialPrepare;
import com.xh.bussiness.resource.dao.MaterialPrepareMapper;
import com.xh.bussiness.resource.service.IMaterialPrepareService;
import com.xh.core.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Pitt
 * @since 2020-12-18
 */
@Service
public class MaterialPrepareServiceImpl extends BaseServiceImpl<MaterialPrepareMapper, MaterialPrepare> implements IMaterialPrepareService {
    @Override
    public XhPage queryMaterialPreparePage(XhPage page, MaterialPrepareRequest bean) {
        QueryWrapper qw=new QueryWrapper<MaterialPrepare>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List queryMaterialPrepare(MaterialPrepareRequest bean) {
        QueryWrapper qw=new QueryWrapper<MaterialPrepare>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return this.dao.selectList(qw);
    }

    @Override
    public Map<String, Long> queryCountByType(String courseIndexId,String userId) {
        Map<String,Long> result = new HashMap<>();
        result.put(MaterialType.COURSEWARE,0L);
        result.put(MaterialType.HANDOUT,0L);
        result.put(MaterialType.MEDIA,0L);
        result.put(MaterialType.OTHER,0L);
        result.put(MaterialType.TEACHPLAN,0L);
        List<Map<String,Object>> maps = this.dao.queryCountByType(courseIndexId, userId);
        Long totalCount = 0L;
        for(Map<String,Object> map : maps){
            Integer type = (Integer) map.get("type");
            Long count = (Long) map.get("count");
            if(type == null){
                continue;
            }
            totalCount +=count;
            if(type == 1){
                result.put(MaterialType.COURSEWARE,count);
            }else if(type == 2){
                result.put(MaterialType.HANDOUT,count);
            }else if(type == 3){
                result.put(MaterialType.MEDIA,count);
            }else if(type == 4){
                result.put(MaterialType.OTHER,count);
            }else if(type == 5){
                result.put(MaterialType.TEACHPLAN,count);
            }
        }
        result.put(MaterialType.ALL,totalCount);
        return result;
    }

    @Override
    public List<MaterialDto> queryMaterialByCourseIndex(String userId, String courseIndexId, Integer type) {
        return this.dao.queryMaterialByCourseIndex(userId, courseIndexId, type);
    }

    @Override
    public void deletePrepareMaterial(String courseIndexId, String materialId) {
        this.dao.deletePrepareMaterial(courseIndexId, materialId);
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(MaterialPrepareRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        return qw;
    }
}
